package com.laoyao.util;
import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;

import org.apache.commons.lang.StringUtils;
//import org.hibernate.validator.xml.GetterType;
/**
 * 图片处理工具类
 * @author hsx
 *
 */
public class ImgCompress {
	public static boolean compressPic(String srcFilePath, String descFilePath,float qality)  {  
        System.out.println(srcFilePath+" == "+srcFilePath);
		File file = null;  
        BufferedImage src = null;  
        FileOutputStream out = null;  
        ImageWriter imgWrier;  
        ImageWriteParam imgWriteParams;  
  
        // 指定写图片的方式为 jpg  
        imgWrier = ImageIO.getImageWritersByFormatName("jpg").next();  
        imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(null);  
        // 要使用压缩，必须指定压缩方式为MODE_EXPLICIT  
        imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);  
        // 这里指定压缩的程度，参数qality是取值0~1范围内，  
        imgWriteParams.setCompressionQuality(qality);  
        imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);  
        ColorModel colorModel = ColorModel.getRGBdefault();  
        // 指定压缩时使用的色彩模式  
        imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel, colorModel  
                .createCompatibleSampleModel(16, 16)));  
  
        try  
        {  
            if(StringUtils.isBlank(srcFilePath))  
            {  
                return false;  
            }  
            else  
            {  
                file = new File(srcFilePath);  
                src = ImageIO.read(file);  
                out = new FileOutputStream(descFilePath);  
  
                imgWrier.reset();  
                // 必须先指定 out值，才能调用write方法, ImageOutputStream可以通过任何 OutputStream构造  
                imgWrier.setOutput(ImageIO.createImageOutputStream(out));  
                // 调用write方法，就可以向输入流写图片  
                imgWrier.write(null, new IIOImage(src, null, null), imgWriteParams);  
                File dbFile = new File(srcFilePath);//创建文件对象
				if (dbFile.exists()) {
//					dbFile.delete();//删除原图，新旧文件名字相同不用删除旧文件
				}
                out.flush();  
                out.close();  
            }  
        }  
        catch(Exception e)  
        {  
            e.printStackTrace();  
            return false;  
        }  
        return true;  
    }  
//2在ImageUtil类中，新建一个main方法，代码如下：
/**
* @param args
 * @throws IOException 
 * @throws FileNotFoundException 
*/
	public static void main(String[] args) throws FileNotFoundException, IOException {
		/****
		* 第一个参数是原图片
		* 第二个参数是压缩后的图片
		* 第三个图片是压缩比例C://Users/hsx/Desktop/新建文件夹 (2)/F_MKXSBNT)Z1A51HD6P[2Y7
		*/
		System.out.println(compressPic("C://Users/hsx/Desktop/新建文件夹 (2)/2.jpg", "C://Users/hsx/Desktop/新建文件夹 (2)/2.jpg",0.1f));
		File picture = new File("C://Users/hsx/Desktop/新建文件夹 (2)/2.jpg");//创建图片对象
		BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture)); 
		System.out.println(String.format("%.1f",picture.length()/1024.0));//文件大小
		System.out.println(sourceImg.getWidth());
		System.out.println(sourceImg.getHeight());
		System.out.println(getMimeType("C://Users/hsx/Desktop/新建文件夹 (2)/20170208164936253.tmp"));		
		System.out.println(getMimeType("C://Users/hsx/Desktop/新建文件夹 (2)/wx_camera_1485520906384.mp4"));
		System.out.println(getMimeType("C://Users/hsx/Desktop/新建文件夹 (2)/2.jpg"));
		}  
	/*
	 * 判断是否为图片还是视频wx_camera_1485520906384.mp4
	 */
	public static Boolean getMimeType(String fileExt) {
		   boolean flay=true;
		   String[] shiPing={"wmv","asf","asx","rm","rmvb","mpg","mpeg","mpe","3gp","mov","mp4","m4v","avi","dat","mkv","flv","vob"};
		   for (int i = 0; i < shiPing.length; i++) {
	         String houzui=shiPing[i];
	         if (fileExt.equals(houzui)) {
	        	 flay=false;
	        	 break;
			 }	      
		   }
		  return flay;
		}  


}
